# Maintainer: David Runge <dvzrv@archlinux.org>
# Maintainer: Sven-Hendrik Haase <svenstaro@archlinux.org>
# Maintainer: Robin Candau <antiz@archlinux.org>
# Contributor: Bartłomiej Piotrowski <bpiotrowski@archlinux.org>
# Contributor: Daniel Wallace <danielwallace at gtmanfred dot com>
# Contributor: Chris <seitz.christoph@gmail.com>
# Contributor: m0ikz <ndelatorre@moikz.com.ar>
# Contributor: atweiden <archbaum@gmail.com>

pkgname=ansible-core
pkgver=2.14.5
pkgrel=1
pkgdesc='Radically simple IT automation platform'
arch=('any')
url='https://pypi.org/project/ansible-core'
license=('GPL3')
depends=(
  'python'
  'python-cryptography'
  'python-jinja'
  'python-packaging'
  'python-pyyaml'
  'python-resolvelib'
  # not directly required, but either convenient or indirectly required
  'python-paramiko'
  'python-typing-extensions'
)
makedepends=(
  'python-build'
  'python-docutils'
  'python-installer'
  'python-setuptools'
  'python-straight.plugin'
  'python-wheel'
)
checkdepends=(
  'git'
  'openssh'
  'python-bcrypt'
  'python-botocore'
  'python-passlib'
  'python-pexpect'
  'python-py'  # missing as dependency of python-pytest-forked
  'python-pytest'
  'python-pytest-mock'
  'python-pytest-xdist'
  'python-pytest-forked'
  'python-pywinrm'
  'python-voluptuous'
)
optdepends=(
  'python-dnspython: for dig lookup'
  'python-jmespath: json_query support'
  'python-netaddr: for the ipaddr filter'
  'python-passlib: crypt values for vars_prompt'
  'python-pip: for module to manage Python libarary dependencies'
  'python-pywinrm: connect to Windows machines'
  'python-setuptools: for module to manage Python libarary dependencies'
  'python-systemd: for journald support'
  'sshpass: for ssh connections with password'
)
provides=('python-ansible' 'ansible-base')
replaces=('ansible-base')
backup=('etc/ansible/ansible.cfg')
source=(
  "https://pypi.python.org/packages/source/a/ansible-core/ansible-core-${pkgver}.tar.gz"
  "$pkgname-2.14.4-resolvelib_1.1.patch"
)
sha512sums=('130a0de925c7229dee350ddbf4382d87802d86aab4817ed595105db8ed5071153847dac0478f20a994670e59e53a07b77d0bf35855d309ebc222fbb61233b7db'
            'cdf1335d49ecec7bb4fff6b29cc0217d0dfa50ca9dc23338717bcbf807d06e7db8bb2c8037f8c613299f92d7db8733ce3e08f343dac33def3bf1612bd6e6bfca')
b2sums=('d006f773e1c2b28582143a4f08f699e5744fba492130388fa5463053cdf8f0cc7056296daa213c72884ea0c0ba454a3e04ef118c184faba8c8d3c281ae65345a'
        '712bc0366da99e8176bd80b7abbc2cdc3d9960911a39fedb7f1ae460577151bf13af9ca1225c28fa63c8feae30226cbc686765511459b81bc253745bf28ae246')

prepare() {
  # backport (minus test requirements!) of patch to add resolvelib < 1.1.0 support
  # https://github.com/ansible/ansible/commit/b148fd8dd74c8599f809f71117a86577ccfb0638
  # https://github.com/ansible/ansible/commit/016b7f71b10539c90ddbb3246f19f9cbf0e65428
  patch -Np1 -d $pkgname-$pkgver -i ../$pkgname-2.14.4-resolvelib_1.1.patch
}

build() {
  cd $pkgname-$pkgver
  python -m build --wheel --no-isolation

  make docs
}

check() {
  local python_version=$(python -c 'import sys; print(".".join(map(str, sys.version_info[:2])))')

  # tests require upstream wrapper to find ansible-core internals: https://github.com/ansible/ansible/issues/80472
  cd $pkgname-$pkgver
  # we do not have libselinux packaged
  rm -v test/units/module_utils/basic/test_selinux.py
  bin/ansible-test units --python $python_version --truncate 0
}

package() {
  cd $pkgname-$pkgver
  python -m installer --destdir="$pkgdir" dist/*.whl

  install -Dm644 COPYING -t "${pkgdir}"/usr/share/doc/${pkgname}/
  install -Dm644 examples/ansible.cfg -t "${pkgdir}"/etc/ansible/
  install -Dm644 examples/{ansible.cfg,hosts} -t "${pkgdir}"/usr/share/ansible/doc/examples/
  install -Dm644 docs/man/man1/*.1 -t "${pkgdir}"/usr/share/man/man1/
}
